﻿using HqlIntellisense;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;

namespace HQLAddin.Intellisense
{
	public class HqlCompletion : Completion
	{
		//private const int SnippetGlyph = 205;
		//public const int ClassGlyph = 0;
		//public const int FunctionGlyph = 72;

		private readonly HQLCompletionProposal completionProposal;
		private readonly IGlyphService glyphService;

		public HqlCompletion(HQLCompletionProposal completionProposal, IGlyphService glyphService)
		{
			this.completionProposal = completionProposal;
			this.glyphService = glyphService;
		}


		public override string DisplayText
		{
			get
			{
				switch (completionProposal.CompletionKind)
				{
					case CompletionKind.EntityName:
						return completionProposal.SimpleName;
					case CompletionKind.AliasRef:
						string simpleName = completionProposal.SimpleName;
						if (string.IsNullOrEmpty(simpleName))
							return completionProposal.ShortEntityName;
						return completionProposal.ShortEntityName + " : " + simpleName;
					case CompletionKind.KeyWord:
						return completionProposal.Completion;
					case CompletionKind.Property:
						return completionProposal.PropertyName;
					case CompletionKind.Function:
						return completionProposal.SimpleName;
					default:
						return string.Empty;
				}
			}
		}


		public override string InsertionText
		{
			get { return GetCompletionSuggestion(); }
		}

		public override string Description
		{
			get { return string.Empty; }
		}

		public override System.Windows.Media.ImageSource IconSource
		{
			get
			{
				switch (completionProposal.CompletionKind)
				{
					case CompletionKind.EntityName:
						return glyphService.GetGlyph(StandardGlyphGroup.GlyphGroupClass, StandardGlyphItem.GlyphItemPublic);
					case CompletionKind.Property:
						return glyphService.GetGlyph(StandardGlyphGroup.GlyphGroupProperty, StandardGlyphItem.GlyphItemPublic);
					case CompletionKind.Function:
						return glyphService.GetGlyph(StandardGlyphGroup.GlyphGroupMethod, StandardGlyphItem.GlyphItemPublic);
					case CompletionKind.AliasRef:
						return glyphService.GetGlyph(StandardGlyphGroup.GlyphGroupVariable, StandardGlyphItem.GlyphItemPrivate);
				}
				return null;
			}
		}

		private string GetCompletionSuggestion()
		{
			switch (completionProposal.CompletionKind)
			{
				case CompletionKind.EntityName:
					return completionProposal.SimpleName;
				case CompletionKind.AliasRef:
					return completionProposal.SimpleName;
				case CompletionKind.Property:
					return completionProposal.PropertyName;
				case CompletionKind.KeyWord:
					return completionProposal.Completion;
				case CompletionKind.Function:
					return completionProposal.SimpleName + "(";
				default:
					return string.Empty;
			}
		}

		
		//public string Name { get { return GetCompletionSuggestion(); } }


		//public int Glyph
		//{
		//    get
		//    {
		//        switch (completionProposal.CompletionKind)
		//        {
		//            case CompletionKind.EntityName:
		//            case CompletionKind.AliasRef:
		//                return ClassGlyph;
		//            default:
		//                return FunctionGlyph;
		//        }
		//    }
		//}

		//public int Priority { get { return completionProposal.Relevance; } }
	}
}